//
//  GGResponseResult.m
//  cicada
//
//  Created by iOSer on 2016/12/14.
//  Copyright © 2016年 iOSer. All rights reserved.
//


#import "GGResponseResult.h"
#import <YYKit/YYKit.h>


@implementation GGResponseResult


//网络连接错误
+(instancetype)resultWithTask:(NSURLSessionDataTask *)task error:(NSError *)error{
    GGResponseResult *responseResult = [[self alloc] init];
    
    responseResult.task = task;
    responseResult.error = error;
    responseResult.showMessage = @"网络连接有误";
    if (error.userInfo[@"com.alamofire.serialization.response.error.response"]!=nil) {
        responseResult.responseObject = error.userInfo[@"com.alamofire.serialization.response.error.response"];
    }
    
    if (error.code == GGResponseResultTypeResponseDataFormatError) {
        responseResult.resultStateType = GGResponseResultTypeResponseDataFormatError;
        responseResult.logMessage = GGResponseResultTypeResponseDataFormatErrorMassage;
    }else{
        responseResult.resultStateType = GGResponseResultTypeNetworkError;
        responseResult.logMessage = GGResponseResultTypeNetworkErrorMassage;
    }
    
    return responseResult;
}


//网络请求成功
+(instancetype)resultWithTask:(NSURLSessionDataTask *)task responseObject:(id)responseObject{
    GGResponseResult *responseResult = [[self alloc] init];
    
    responseResult.task = task;
    responseResult.responseObject = responseObject;
    responseResult.showMessage = responseObject[@"message"];
    responseResult.pageSize = [responseObject[@"pageSize"] integerValue];
    switch ([responseObject[CODE_KEY] integerValue]) {
        case 0://请求失败
            responseResult.logMessage = @"参数错误";
            responseResult.resultStateType = GGResponseResultTypeError;
            responseResult.error = [NSError errorWithDomain:CODE_INFO code:GGResponseResultTypeRequestDataFormatError userInfo:@{@"task":task}];
            break;
        case 1://请求成功
            if (responseObject[DATA_KEY] == [NSNull alloc]) {
                //数据为空
                responseResult.logMessage = GGResponseResultTypeResponseDataZeroTrueMessage;
                responseResult.resultStateType = GGResponseResultTypeResponseDataZero;
                responseResult.error = [NSError errorWithDomain:CODE_INFO code:GGResponseResultTypeResponseDataZero userInfo:@{@"task":task}];
            }else{
                responseResult.logMessage = GGResponseResultTypeSuccessMassage;
                responseResult.resultStateType = GGResponseResultTypeSuccess;
            }
            break;
    }
    
    return responseResult;
}

#pragma mark - getter && setter
-(void)setModelClass:(Class)modelClass{
    _modelClass = modelClass;
    if (modelClass) {
        if (self.responseObject) {
            if (self.modelAnalysisKey) {
                self.model = [modelClass modelWithJSON:self.responseObject[self.modelAnalysisKey]];
            }else{
                self.model = [modelClass modelWithJSON:self.responseObject[DATA_KEY]];
            }
        }
    }
}

@end
